package com.tomato.drugstore.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tomato.drugstore.VO.ResultVO;
import com.tomato.drugstore.entity.Drug;
import com.tomato.drugstore.mapper.DrugsMapper;
import com.tomato.drugstore.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "药品")
@RestController
@RequestMapping("/api/drugs")
public class DrugControl {
    @Resource
    private DrugsMapper drugsMapper;

    @ApiOperation("获取分页药物列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = false,
                    dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "大小", required = false,
                    dataType = "Integer", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "drugName", value = "药名", required = false,
                    dataType = "string", paramType = "query", defaultValue = ""),
    })
    @GetMapping("/drugsPage")
    public ResultVO drugsPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "size", defaultValue = "10") Integer size,
                              @RequestParam(value = "drugName", defaultValue = "") String drugName) {
        Page<Drug> drugPage = new Page<>(page, size);
        IPage<Drug> drugIPage = drugsMapper
                .selectPage(drugPage, new QueryWrapper<Drug>()
                        .like("drug_name", drugName));
        return R.success(drugIPage);
    }
}
